<?php

namespace App\Http\Controllers;

use App\Transformer\LessonTransformer;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Lesson;

class LessonsController extends ApiController
{
    protected $lessonTransformer;

    public function __construct(LessonTransformer $lessonTransformer)
    {
        $this->lessonTransformer = $lessonTransformer;
        $this->middleware('auth.basic', ['only' => ['store', 'update']]);//指定方法验证
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $lessons = Lesson::all();
//        return \Response::json([
//           'status'=>'success',
//            'status_code'=>200,
//            //'data'=>$lessons->toArray()  1
//            //'data'=>$this->transformCollection($lessons)  2
//            'data'=>$this->lessonTransformer->transformCollection($lessons->toArray()) //3
//        ]);
        return $this->response([
            'status' => 'success',
            'data' => $this->lessonTransformer->transformCollection($lessons->toArray())
        ]);
        //return Lesson::all();
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {

    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //dd($request->get('body'));die;
        if (!$request->get('title') or !$request->get('body')) {
            return $this->setStatusCode(422)->responseError('validate fails');
        }
        Lesson::create($request->all());
        return $this->setStatusCode(201)->response([
            'status' => 'success',
            'message' => 'lesson created',
        ]);
    }

    /**
     * Display the specified resource.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //$lesson=Lesson::findOrFail($id);
        $lesson = Lesson::find($id);
        if (!$lesson) {
            //return $this->responseNotFound();
            //return $this->setStatusCode(404)->responseNotFound();
            return $this->responseNotFound();
        }
//        return \Response::json([
//            'status'=>'success',
//            'status_code'=>200,
//            //'data'=>$lesson->toArray()
//            'data'=>$this->lessonTransformer->transform($lesson)
//        ]);
        return $this->response([
            'status' => 'success',
            //'status_code'=>200,
            //'data'=>$lesson->toArray()
            'data' => $this->lessonTransformer->transform($lesson)
        ]);
        //return $lesson;
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
//    public function transform($lessons)
//    {
//        return array_map(function($lesson){
//            return [
//                'title'=>$lesson['title'],
//                'content'=>$lesson['body'],
//                'is_free'=>$lesson['free']
//            ];
//        },$lessons->toArray());
//    }
//复制到transformer类
//    private function transformCollection($lessons)
//    {
//        return array_map([$this,'transform'],$lessons->toArray());
//    }
    //LessonTransformer
//    private function transform($lesson)
//    {
//        return [
//            'title'=>$lesson['title'],
//            'content'=>$lesson['body'],
//            'is_free'=>(boolean)$lesson['free']
//        ];
//    }
}
